package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpCarTireArchives;
import com.monitor.entity.OpeningCarTire;
import com.monitor.service.ErpCarTireArchivesService;
import com.monitor.service.ErpCustomerCarService;
import com.monitor.service.ErpInspectionContentService;
import com.monitor.service.ErpTireSensorService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆轮胎档案表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-27
 */
@RestController
@RequestMapping("/erpCarTireArchives")
@Api(description="车辆轮胎表")
public class ErpCarTireArchivesController {
    @Autowired
    private ErpTireSensorService erpTireSensorService;

    @Resource
    private ErpCarTireArchivesService archivesService;
    @Resource
    private ErpCustomerCarService carService;
    
    @Autowired
    private ErpInspectionContentService erpInspectionContentService;

    @RequestMapping("/inertOrUpdateTireArchives")
    @ApiOperation("添加Or修改车辆轮胎信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateTireArchives(ErpCarTireArchives Model) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(archivesService.insertOrUpdate(Model)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getTireArchivesById")
    @ApiOperation("根据车辆轮胎id车辆轮胎信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "archivesId", value = "车辆轮胎ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpCarTireArchives> getTireArchivesById(Integer archivesId) {
        Result<ErpCarTireArchives> result=new Result<ErpCarTireArchives>();
        try{
            result.setCount(1);
            result.setData(archivesService.selectById(archivesId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeTireArchivesById")
    @ApiOperation("根据车辆轮胎id删除车辆轮胎信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "archivesId", value = "车辆轮胎ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeTireArchivesById(Integer archivesId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(archivesService.deleteById(archivesId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getTireArchivesByarchivesIdPage")
    @ApiOperation("查询车辆轮胎分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "archivesId", value = "合同ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Page<ErpCarTireArchives>> getTireArchivesBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                        @RequestParam(value="limit",defaultValue="10") int limit) {
        Result<Page<ErpCarTireArchives>> result=new Result<Page<ErpCarTireArchives>>();
        try{
            Page<ErpCarTireArchives> page1=new Page<ErpCarTireArchives>(page,limit);
            Page<ErpCarTireArchives> carPage=archivesService.selectPage(page1,new EntityWrapper<ErpCarTireArchives>());
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getTireArchivesByarchivesIdList")
    @ApiOperation("查询所有车辆轮胎")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "archivesId", value = "合同ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<List<ErpCarTireArchives>> getTireArchivesBycustIdList() {
        Result<List<ErpCarTireArchives>> result=new Result<List<ErpCarTireArchives>>();
        try{
            List<ErpCarTireArchives> carPage=archivesService.selectList(new EntityWrapper<ErpCarTireArchives>());
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

//    @RequestMapping("/getTireByCarId")
//    @ApiOperation("统计管理-车辆物料")
//    @ApiImplicitParams({
//           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
//            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "carId", value = "车辆ID", required = false, dataType = "int", paramType = "query")
//    })
//    public @ResponseBody
//    Result<List<Map<String,Object>>> getTireByCarId(@RequestParam(value = "carId", defaultValue = "0") Integer carId,Integer custId) {
//        Result<List<Map<String,Object>>> result=new Result<List<Map<String,Object>>>();
//        try{
//            String carIdStr=null==carId||0==carId?"":carId.toString();
//            if (StringUtils.isEmpty(carIdStr)){
//                List<ErpCustomerCar> cars=carService.selectList(new EntityWrapper<ErpCustomerCar>().eq("customer_id",custId));
//                if(0==cars.size()){
//                    result.setCount(0);
//                    result.setData(null);
//                    result.setMsg("数据为空");
//                    return result;
//                }
//                List<Integer> carIds=cars.stream().map(ErpCustomerCar::getId).collect(Collectors.toList());
//                carIdStr= BlankUtil.listToString(carIds);
//            }
//            List<Map<String, Object>> mapList= archivesService.getTireByCarId(carIdStr);
//            result.setCount(mapList.size());
//            result.setData(mapList);
//        }catch (Exception e){e.printStackTrace();}
//        return result;
//    }
	@RequestMapping("/getTireByCarId")
	@ApiOperation("统计管理-车辆物料")  //789456
	@ApiImplicitParams({
	           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
	            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
	            @ApiImplicitParam(name = "startDate", value = "开始时间", required = false, dataType = "String", paramType = "query"),
	            @ApiImplicitParam(name = "endDate", value = "结束时间", required = false, dataType = "String", paramType = "query"),*/
	        @ApiImplicitParam(name = "carPlate", value = "车辆牌号", required = false, dataType = "String", paramType = "query")
	})
	public Result<List<OpeningCarTire>> getTireByCarId(String carPlate){
	
	    return erpTireSensorService.getTireByCarId(carPlate);
	}
	
	@RequestMapping("/selectByWuLiaoDangAn")
	@ApiOperation("统计管理-车辆物料-物料档案查询")
	@ApiImplicitParam(name = "number", value = "生产编号", required = false, dataType = "String", paramType = "query")
	public Result<Map<String,Object>> selectByWuLiaoDangAn(String number,String carPlate,String name,String location){
		return erpInspectionContentService.selectByWuLiaoDangAn(number,carPlate,name,location);
	}

    @RequestMapping("/selectByCarPlate")
    @ApiOperation("根据车id查询轮胎列表")
    public Result<List<ErpCarTireArchives>> selectByCarPlate(int carId){
    	//ErpCustomerCar car=carService.selectByCarPlate(plate);
    	/*if(car==null) {
    		return new Result<>(1, "未查询到该车辆信息");
    	}*/
    	Result<List<ErpCarTireArchives>> result=new Result<List<ErpCarTireArchives>>();
        try{
            List<ErpCarTireArchives> carPage=archivesService.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", carId));

            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }
}

